package com.xudow.app.dynamicstate_old.module.follow;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.activeshare.edu.ucenter.common.messages.Message;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jude.beam.expansion.BeamBaseActivity;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.HeaderInterceptors;
import com.xudow.app.dynamicstate_old.data.server.ServiceAPI;
import com.xudow.app.dynamicstate_old.domain.entity.Report;
import com.xudow.app.dynamicstate_old.widget.ContainsEmojiEditText;
import com.xudow.app.dynamicstate_old.widget.MyRadioGroup;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.schedulers.Schedulers;


/**
 * Created by troyl on 2016/5/6.
 * 举报
 */
public class ReportActivity extends BeamBaseActivity {

    public static final String USER_PROFILE_ID = "user_profile_id"; // 被举报人的ID，不是被举报人的
    public static final String REPORTED_DYNAMIC_ID = "dynamic_id";
    public static final String REPORTED_USER_NAME = "reported_user_name";
    public static final String REPORTED_CONTENT = "reported_content";

    @BindView(R.id.dynamic_activity_report_title)
    TextView mTitle;
    @BindView(R.id.dynamic_activity_report_dynamic_content)
    TextView mDynamicContent;
    //    @BindView(R.id.dynamic_activity_report_left_radiogroup)
//    RadioGroup mLeftRadioGroup;
//    @BindView(R.id.dynamic_activity_report_right_radiogroup)
//    RadioGroup mRightRadioGroup;
    @BindView(R.id.dynamic_activity_report_content)
    ContainsEmojiEditText mEditContent;
    @BindView(R.id.radio1)
    RadioButton radio1;
    @BindView(R.id.radio2)
    RadioButton radio2;
    @BindView(R.id.radio3)
    RadioButton radio3;
    @BindView(R.id.radio4)
    RadioButton radio4;
    @BindView(R.id.radio5)
    RadioButton radio5;
    @BindView(R.id.radio6)
    RadioButton radio6;
    @BindView(R.id.radio_group)
    MyRadioGroup radioGroup;
    @BindView(R.id.dynamic_activity_report_submit)
    Button dynamicActivityReportSubmit;

    // 提交report请求所需要的数据
    private long mDynamicId;
    private int mReason = 1;
    private long mUserProfileId;
    private String mRemark;

    // 显示在Activity上所需要的数据
    private String mReportedUserName;
    private String mReportedContent;
    XApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamic_activity_report);
        ButterKnife.bind(this);
        initData();
        initListener();
    }

    @OnClick(R.id.dynamic_activity_report_submit)
    public void submit(View view) {
        mRemark = mEditContent.getText().toString().trim();
        if (mRemark.isEmpty()) {
            JUtils.Toast("请填写举报内容");
            return;
        }
        getExpansion().showProgressDialog("提交中");
        Report report = new Report();
        report.setDynamicId(mDynamicId);
        report.setReason(mReason);
        report.setRemark(mRemark);
        report.setUserProileId(app.getUserProfileId());
        report.setReporterId(mUserProfileId);
        report.setReporterName(mReportedUserName);
        report(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(report));
//        DynamicModel.getInstance().report(new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
//                .create().toJson(report))
//                .subscribe(message -> {
//                    JUtils.Toast("举报成功");
//                    finish();
//                });
    }

    private void report(String json) {
        RequestBody body = new FormBody.Builder()
                .add("report", json)
                .build();
        Request request = new Request.Builder()
                .url(ServiceAPI.BASE_URL + "v1/app/dynamic/submit_report")
                .post(body)
                .build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new HeaderInterceptors())
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                getExpansion().dismissProgressDialog();
                JUtils.Toast("提交失败，请稍后重试");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = new Gson().fromJson(response.body().string(), Message.class);
                runOnUiThread(() -> {
                    getExpansion().dismissProgressDialog();
                    if (message.getResult().equals("0")) {
                        JUtils.Toast("提交成功");
                        finish();
                    } else {
                        JUtils.Toast(message.getErrorMessage());
                    }
                });
            }
        });
    }

    protected void initData() {
        app = (XApplication) getApplication();
        Intent intent = getIntent();
        mDynamicId = intent.getLongExtra(REPORTED_DYNAMIC_ID, 0);
        mUserProfileId = intent.getLongExtra(USER_PROFILE_ID, 0);
        mReportedUserName = intent.getStringExtra(REPORTED_USER_NAME);
        mReportedContent = intent.getStringExtra(REPORTED_CONTENT);
        SpannableString spannableTitle = new SpannableString("举报@" + mReportedUserName + "学兜动态：");
        spannableTitle.setSpan(new ForegroundColorSpan(Color.GREEN), 2, 2 + mReportedUserName.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableTitle.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // TODO 跳转到用户详情界面
            }
        }, 2, 2 + mReportedUserName.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTitle.setText(spannableTitle);
        SpannableString spannableContent = new SpannableString("@" + mReportedUserName + ":" + (mReportedContent.length() > 10 ? mReportedContent.substring(0, 10) + "..." : mReportedContent));
        spannableContent.setSpan(new ForegroundColorSpan(Color.GREEN), 0, mReportedUserName.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableContent.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // TODO 跳转到用户详情界面
            }
        }, 0, mReportedUserName.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mDynamicContent.setText(spannableContent);
    }

    protected void initListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio1:
                        mReason = 1;
                        break;
                    case R.id.radio2:
                        mReason = 2;
                        break;
                    case R.id.radio3:
                        mReason = 3;
                        break;
                    case R.id.radio4:
                        mReason = 4;
                        break;
                    case R.id.radio5:
                        mReason = 5;
                        break;
                    case R.id.radio6:
                        mReason = 6;
                        break;
                }
            }
        });
    }

    @OnClick(R.id.image_back)
    public void cancelReport(View view) {
        finish();
    }
}
